package com.vito.oa.act.extend.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONArray;
import com.vito.act.model.ActExUserDefinedPro;
import com.vito.act.service.impl.ActExUserDefinedProServiceImpl;
import com.vito.base.authoriza.model.UserModel;
import com.vito.base.model.DataGrid;
import com.vito.base.model.MyHashMap;
import com.vito.base.util.Contants;
import com.vito.base.util.JSONUtil;
import com.vito.base.util.Validata;
import com.vito.oa.act.constant.OaActContants;
import com.vito.oa.act.extend.dao.OaActExProcessBranchMapper;
import com.vito.oa.act.extend.dao.OaActExUserDefinedProMapper;
import com.vito.oa.act.extend.model.OaActExUserDefinedPro;
import com.vito.oa.act.extend.service.OaActExUserDefinedProService;
@Service(group="OaActExUserDefinedProServiceImpl")
public class OaActExUserDefinedProServiceImpl extends ActExUserDefinedProServiceImpl implements OaActExUserDefinedProService {

	@Autowired
	private OaActExUserDefinedProMapper userDefinedProMapper;
	@Autowired
	private OaActExProcessBranchMapper branchMapper;
	
	
	@Override
	public DataGrid<ActExUserDefinedPro> queryActExUserDefinedProByPage(ActExUserDefinedPro userDefinedPro) {
		DataGrid<ActExUserDefinedPro> dg = new DataGrid<ActExUserDefinedPro>();
		dg.setRows(userDefinedProMapper.queryActExUserDefinedProByPage(userDefinedPro));
		dg.setTotal(userDefinedPro.getTotal());
		return dg;
	}
	@Override
	public List<MyHashMap<String, String>> getUserTree(String[] checkeds,
			UserModel user) {
		List<MyHashMap<String,String>> list = new ArrayList<MyHashMap<String, String>>();
		list = userDefinedProMapper.queryDeptUserTree(user.getDeptId());
		return list;
	}
	@Override
	public Integer setDealUsers(OaActExUserDefinedPro userDefinedPro) {
		try{
		//1 删除上次设置数据
		String dfId = userDefinedProMapper.selectActExUserDefinedId(userDefinedPro);
		if(Validata.notNullOrEmpty(dfId)){
			userDefinedPro.setDefinedId(dfId);
			userDefinedProMapper.deleteActExUserDefined(userDefinedPro);
		}
		//2 重新插入最新设置的数据
		// 初始化参数
		userDefinedPro.setStatus(Contants.BASE_STATE);
		String setUsers = userDefinedPro.getSetUserIds();
		if(!Validata.isNullOrEmpty(setUsers)){
			JSONArray array = JSONUtil.convertString2JsonArray(setUsers);
			if(array!=null&&array.size()>0){
				if(array!=null&&array.size()>0){
					//Set<Map<String,String>> useriIds = new HashSet<Map<String,String>>();
					for (int i = 0; i < array.size(); i++) {
						userDefinedPro.setUserType(array.getJSONObject(i).getString("usertype"));
						if(i<(array.size()-1)){
							if(null != userDefinedPro.getUserId()){
								userDefinedPro.setUserId(userDefinedPro.getUserId()+array.getJSONObject(i).getString("userid")+OaActContants.SEPARATOR);
							}else{
								userDefinedPro.setUserId(array.getJSONObject(i).getString("userid")+OaActContants.SEPARATOR);
							}
							
						}else{
							if(null != userDefinedPro.getUserId()){
								userDefinedPro.setUserId(userDefinedPro.getUserId()+array.getJSONObject(i).getString("userid"));
							}else{
								userDefinedPro.setUserId(array.getJSONObject(i).getString("userid"));
							}
							
						}
					}
				}
				userDefinedProMapper.saveActExUserDefined(userDefinedPro);
			}
		}
		}catch(Exception e){
			return -1;
		}
		return 1;
	}
	@Override
	public Integer updateActExUserDefinedPro(ActExUserDefinedPro userDefinedPro) {
		return userDefinedProMapper.updateActExUserDefined(userDefinedPro);
	}
	@Override
	public List<MyHashMap<String, String>> getUserImgs(OaActExUserDefinedPro userDefinedPro) {
		List<MyHashMap<String, String>> listData = userDefinedProMapper.getUserImgs(userDefinedPro);
		if(listData!=null){
			List<MyHashMap<String, String>> newData = new ArrayList<MyHashMap<String, String>>();
			for(MyHashMap<String, String> data : listData){
				String userid = data.get("userid");
				if(null == userid ){
					continue;
				}
				if( userid.contains(OaActContants.SEPARATOR)){
					String userids[] = userid.split(OaActContants.SEPARATOR);
					List<Map<String,String>> userIdList = new ArrayList<Map<String,String>>();
					for(String id :userids){
						Map<String,String> userIdMap = new HashMap<String,String>();
						userIdMap.put("user_id", id);
						userIdMap.put("user_type", data.get("usertype"));
						userIdList.add(userIdMap);
					}
					List<MyHashMap<String, String>> result = branchMapper.trUserInfo(userIdList);
					if (null != result && !result.isEmpty())
						for (int i = 0; i < result.size(); i++) {
							MyHashMap<String, String> resultData = result.get(i);
							resultData.put("userid", userids[i]);
							resultData.put("usertype", data.get("usertype"));
							newData.add(resultData);
						}

				}else{
					newData.add(data);
				}
			}
			return newData;
		}
		return listData;
	}
	
}